import time

from PIL import ImageGrab
from cvzone.FaceMeshModule import FaceMeshDetector
import numpy as np
import cv2

# cv2.waitKey(1)

# detector = HandDetector(detectionCon=0.5)
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(minDetectionCon=0.4)

while True:
    ImageGrab.grab().save('screen_capture.jpg', "JPEG")
    img = cv2.imread('screen_capture.jpg')
    success, photo = cap.read()
    x, y, _ = photo.shape
    img[100:100+x, 100: 100+y] = photo
    imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    imgRGB, faces = detector.findFaceMesh(imgRGB)
    #  找到图片中的手
    if faces:
        for hand in faces:
            print(hand)
    img = cv2.cvtColor(imgRGB, cv2.COLOR_RGB2BGR)
    img = cv2.resize(img, (1920 // 2, 1080 // 2))

    cv2.imshow("video", img)
    # time.sleep(1)
    key = cv2.waitKey(1)
    if key == ord('q'):
        cv2.destroyAllWindows()
        break
